#!/usr/bin/env python
import os
import urllib2, urllib
from xml.dom import minidom
from pprint import pprint
url = "bluedot.us"

def login(username, password):
    password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
    password_manager.add_password(None, 'https://secure.bluedot.us/v1', username, password)
    auth_handler = urllib2.HTTPBasicAuthHandler(password_manager)
    opener = urllib2.build_opener(auth_handler)
    urllib2.install_opener(opener)
    try: 
        check_status()
        return True
    except urllib2.HTTPError:
        return False
    
def __urlencode(arg):
    for key, value in arg.items():
        if isinstance(value, unicode):
            arg[key] = value.encode('utf-8')
    return urllib.urlencode(arg)

def check_status():
    status = minidom.parseString(urllib2.urlopen('https://secure.bluedot.us/v1/posts/update?').read()).getElementsByTagName('update')
    
    status = minidom.Element.toxml(status[0])
    status = status.split('time=')
    status = status[1].split('"')
    status = status[1]
    try: return status
    except urllib2.HTTPError:
        return False

def add_post(title, description, tags, posturl, shared):
    post = __urlencode({
        'url': posturl,
        'description': title,
        'extended': description,
        'tags': tags,
        'replace': 'no',
        'shared': shared
    })
    try: 
        minidom.parseString(urllib2.urlopen('https://secure.bluedot.us/v1/posts/add?'+post).read()).getElementsByTagName('result')[0].getAttribute('code')
        return True
    except urllib2.HTTPError:
        return False
    
def get_tags():
    mytags = minidom.parseString(urllib2.urlopen('https://secure.bluedot.us/v1/tags/get?').read()).getElementsByTagName('tag')
    
    itemlist = []
    for item in mytags:
        itemint = minidom.Element.toxml(item)
        itemint = itemint.split('tag=')
        itemint = itemint[1].split('"')
        itemint = itemint[1]
        itemlist.append(itemint)
    try: return itemlist
    except urllib2.HTTPError:
        return False

def get_posts():
    myposts = minidom.parseString(urllib2.urlopen('https://secure.bluedot.us/v1/posts/all?').read()).getElementsByTagName('post')
    
    itemlist = []
    for item in myposts:
        itemxml = minidom.Element.toxml(item)
        
        itemint = itemxml.split('description=')
        itemint = itemint[1].split('"')
        itemint = itemint[1]
        itemintlist = []
        itemintlist.append(itemint)
        
        itemurl = itemxml.split('href=')
        itemurl = itemurl[1].split('"')
        itemurl = itemurl[1]
        itemintlist.append(itemurl)
        
        itemtag = itemxml.split('tag=')
        itemtag = itemtag[1].split('"')
        itemtag = itemtag[1]
        itemintlist.append(itemtag)
        
        itemext = itemxml.split('extended=')
        itemext = itemext[1].split('"')
        itemext = itemext[1]
        itemintlist.append(itemext)
        
        itemlist.append(itemintlist)
    try: return itemlist
    except urllib2.HTTPError:
        return False

def get_stats():
    return False
